package cn.edu.neusoft.yuejian4.yuejian_4.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.GameDetailActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Recentgame;


/**
 * Created by Lix on 2018/10/10.
 */

public class GameListAdapter extends RecyclerView.Adapter<GameListAdapter.ViewHolder> {

    private List<Recentgame> mRecentgame;
    private Context mContext;

    public GameListAdapter(Context mContext, List<Recentgame> mRecentgame){
        this.mContext = mContext;
        this.mRecentgame = mRecentgame;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public GameListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_cardview,parent,false);
        GameListAdapter.ViewHolder holder = new GameListAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(GameListAdapter.ViewHolder holder, int position) {

        final Recentgame recentgame = mRecentgame.get(position);
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+recentgame.getPic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.gameImg);

        holder.gameName.setText(recentgame.getName());
        holder.gameAddress.setText(recentgame.getAddress());
        holder.gameImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GameDetailActivity.class);
                intent.putExtra("game_id",recentgame.getId());
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mRecentgame.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView gameImg;
        TextView gameName;
        TextView gameAddress;
        FrameLayout game_container;

        public ViewHolder(View view){
            super(view);
            gameImg= (ImageView)view.findViewById(R.id.game_img);
            gameName = (TextView)view.findViewById(R.id.game_name);
            gameAddress = (TextView)view.findViewById(R.id.game_address);
            game_container = (FrameLayout)view.findViewById(R.id.game_container);
        }
    }

}
